package cn.manager.travel.dao.impl;

import cn.manager.travel.dao.ClientDao;
import cn.manager.travel.domain.Laboratory;
import cn.manager.travel.domain.Major;
import cn.manager.travel.domain.New;
import cn.manager.travel.domain.Teacher;
import cn.manager.travel.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ClientDaoImpl implements ClientDao {
	
	private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
	
	@Override
	public List<New> showNews(int i) {
		String sql = "select * from news limit 0,?";
		List<New> news = null;
		try {
			news = template.query(sql, new BeanPropertyRowMapper<>(New.class),i);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return news;
	}
	
	@Override
	public List<New> showNewList() {
		String sql = "select * from news";
		List<New> news = null;
		try {
			news = template.query(sql, new BeanPropertyRowMapper<>(New.class));
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return news;
	}
	
	@Override
	public New showNew(Integer nID) {
		String sql = "select * from news where nID = ?";
		return 	template.queryForObject(sql,new BeanPropertyRowMapper<>(New.class),nID);
		
	}
	
	@Override
	public List<Major> showMajorList() {
		String sql = "select * from major";
		List<Major> majors = null;
		try {
			majors = template.query(sql, new BeanPropertyRowMapper<>(Major.class));
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return majors;
	}
	
	@Override
	public List<Major> showMajors(int i) {
		String sql = "select * from major limit 0,? ";
		List<Major> majors = null;
		try {
			majors = template.query(sql, new BeanPropertyRowMapper<>(Major.class),i);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return majors;
	}
	
	@Override
	public Major showMajor(int mID) {
		String sql = "select * from major where mID = ?";
		return template.queryForObject(sql,new BeanPropertyRowMapper<>(Major.class),mID);
	}
	
	@Override
	public List<Teacher> showTeachers() {
		String sql = "select * from teacher";
		List<Teacher> teachers = null;
		try {
			teachers = template.query(sql, new BeanPropertyRowMapper<>(Teacher.class));
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return teachers;
	}
	
	@Override
	public Teacher showTeacher(int tID) {
		String sql = "select * from teacher where tID = ?";
		return template.queryForObject(sql,new BeanPropertyRowMapper<>(Teacher.class),tID);
	}
	
	@Override
	public List<Laboratory> showLaboratories() {
		String sql = "select * from laboratory";
		List<Laboratory> laboratorys = null;
		try {
			laboratorys = template.query(sql, new BeanPropertyRowMapper<>(Laboratory.class));
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return laboratorys;
	}
	
	@Override
	public Laboratory showLaboratory(int lID) {
		String sql = "select * from laboratory where lID = ?";
		return template.queryForObject(sql,new BeanPropertyRowMapper<>(Laboratory.class),lID);
	}
}
